<?php

/**
 * Class for viewing a CSS rule. This includes comments, or text strings.
 * The code within the element is an array of such elements.
 *
 * @author Ian Clarence ian.clarence@gmail.com
 */ 
class ViewCssRule {
    
    /**
     * The selector: can be nested and / or grouped.
     * 
     * @var ViewGroupedSelector
     */
    private $viewGroupedSelector;
    
    /**
     * The declaration.
     * 
     * @var ViewDeclaration
     */
    private $viewDelaration;

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct($viewGroupedSelector, $viewDeclaration) {
        if ($viewGroupedSelector instanceof ViewGroupedSelector) {
            $this->viewGroupedSelector = $viewGroupedSelector;
        }
        if ($viewDeclaration instanceof ViewDeclaration) {
            $this->viewDeclaration = $viewDeclaration;
        }        
    }
    
    /**
     * Returns a CSS rule: inputs a CSS selector string which can be nested or grouped.
     * 
     * @param string $selectorString
     * @param array $declarationArray
     * @return ViewCssRule
     */
    public static function cssInit($selectorString = '', $declarationArray = array()) {
        $selector = ViewGroupedSelector::selectorParser($selectorString);
        $declaration = new ViewDeclaration($declarationArray);
        return new self($selector, $declaration);
    }
    
    /**
     * Viewing the CSS rule.
     * 
     * @return string
     */
    public function view() { 
        return $this->viewGroupedSelector->view() . $this->viewDeclaration->view();
    }
    
}